package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.enums.AgeEnums;
import com.xingkeduo.pojo.enums.CareerEnums;
import com.xingkeduo.pojo.query.ConsumerPrivateQO;
import com.xingkeduo.pojo.table.ConsumerPrivateTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.SexEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.RegularUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/3/1 15:02
 **/
@Component
public class ConsumerPrivateJTO extends BaseJson {


    public static ObjectNode toJsonForSendSms(Map<String, Object> map) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (map != null) {
            if ("00".equals(map.get("error"))) {
                root.put("is_new", 0);
            } else if ("01".equals(map.get("error"))) {
                root.put("is_new", 1);
            }
        }
        return root;
    }


    public static ObjectNode toJsonDistance(Map<String, Integer> map) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (map != null) {
            root.put("f_frequency", map.get("f_frequency"));
        }
        return root;
    }


    public static ObjectNode toJsonForUserReviseSet(Map<String, Object> map) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        root.put("f_sex", "");
        root.put("tag_year", "");
        root.put("tag_feel_style", "");
        root.put("tag_job", "");
        root.put("arrive_store_distance", "");
        if (map != null) {
            root.put("f_sex", map.get("f_sex").toString());
            root.put("tag_year", map.get("tag_year").toString());
            root.put("tag_feel_style", map.get("tag_feel_style").toString());
            root.put("tag_job", map.get("tag_job").toString());
            root.put("arrive_store_distance", map.get("arrive_store_distance").toString());
        }

        return root;
    }

    public static ObjectNode toJsonForUserInfo(Map<String, Object> map) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        root.put("f_name", "");
        root.put("days", 0);
        root.put("count", 0);
        root.put("f_sex", 0);
        root.put("f_avatar", "");
        root.put("coupon_count", 0);
        root.put("card_count", 0);
        ObjectNode giftBagInfo = MAPPER.createObjectNode();
        giftBagInfo.put("gift_bag_id", "");
        giftBagInfo.put("type", "");
        ObjectNode card = MAPPER.createObjectNode();
        card.put("id", "");
        card.put("title", "");
        card.put("pic", "");
        card.put("need_check", false);
        card.put("cert_status", "");
        giftBagInfo.put("card", card);
        root.put("gift_bag_info", giftBagInfo);
        root.put("has_no_cert", false);
        root.put("mobile", "");

        if (map != null) {
            if (map.get("f_name") != null) {
                root.put("f_name", StringUtils.isNotEmpty(map.get("f_name").toString()) ? map.get("f_name").toString() : "");
            }
            if (map.get("f_avatar") != null) {
                root.put("f_avatar", StringUtils.isNotEmpty(map.get("f_avatar").toString()) ? map.get("f_avatar").toString() : "");
            }
            if (map.get("mobile") != null) {
                root.put("mobile", StringUtils.isNotEmpty(map.get("mobile").toString()) ? map.get("mobile").toString() : "");
            }
            if (map.get("f_sex") != null) {
                int sex = Integer.valueOf(map.get("f_sex").toString()).intValue();
                if (sex >= 0) {
                    if (sex == 0) {//女
                        root.put("f_sex", 2);
                    } else if (sex == 1) {//男
                        root.put("f_sex", 1);
                    }
                }
            }
            if (map.get("days") != null) {
                root.put("days", Integer.valueOf(map.get("days").toString()));
            }
            if (map.get("count") != null) {
                root.put("count", Integer.valueOf(map.get("count").toString()));
            }

            if (map.get("coupon_count") != null) {
                root.put("coupon_count", Integer.valueOf(map.get("coupon_count").toString()));
            }
            if (map.get("has_no_cert") != null) {
//                root.put("has_no_cert", Boolean.valueOf(map.get("has_no_cert").toString()));
            }

        }
        return root;
    }

    public static ObjectNode toJsonAge(ConsumerPrivateTO consumerPrivateTO) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (consumerPrivateTO != null && consumerPrivateTO.getAgeId() != null) {
            root.put("f_tag_age", consumerPrivateTO.getAgeId());
        } else {
            root.put("f_tag_age", "");
        }

        ArrayNode tagYearList = MAPPER.createArrayNode();
        AgeEnums[] ageS = AgeEnums.values();
        if (ageS != null && ageS.length > 0) {
            for (int i = 0; i < ageS.length; i++) {
                ObjectNode jNode = MAPPER.createObjectNode();
                jNode.put("f_id", ageS[i].getId());
                jNode.put("f_name", ageS[i].getName());
                jNode.put("f_alias_name", ageS[i].getAliasName());
                jNode.put("f_remark", ageS[i].getRemark());
                tagYearList.add(jNode);
            }
        }
        root.put("tag_year", tagYearList);

        return root;
    }

    public static ObjectNode toJsonWork(ConsumerPrivateTO consumerPrivateTO) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        Integer ss = null;
        if (consumerPrivateTO != null && consumerPrivateTO.getCareerId() != null) {
            root.put("f_professional_attributes", consumerPrivateTO.getCareerId());
        } else {
            root.put("f_professional_attributes", ss);
        }

        ArrayNode tagJobList = MAPPER.createArrayNode();

        CareerEnums[] careerS = CareerEnums.values();
        if (careerS != null && careerS.length > 0) {
            for (int i = 0; i < careerS.length; i++) {
                ObjectNode jNode = MAPPER.createObjectNode();
                jNode.put("f_id", careerS[i].getId());
                jNode.put("f_name", careerS[i].getName());
                jNode.put("f_remark", careerS[i].getRemark());
                tagJobList.add(jNode);
            }
        }
        root.put("tag_job_list", tagJobList);

        return root;
    }

    public static ObjectNode toJsonSex(ConsumerPrivateTO consumerPrivateTO) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        root.put("f_sex", 0);
        if (consumerPrivateTO != null && consumerPrivateTO.getSex() != null) {
            if (SexEnum.FEMALE.equals(consumerPrivateTO.getSex())) {
                root.put("f_sex", 2);
            } else if (SexEnum.MALE.equals(consumerPrivateTO.getSex())) {
                root.put("f_sex", 1);
            }
        }
        return root;
    }

    public static ConsumerPrivateQO createQueryCondition(String json) throws Throwable {
        ConsumerPrivateQO query = new ConsumerPrivateQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;
        if (root.has("sex")) {
            query.setSex(validSex(root, YesNoEnum.NO));
        }

        if (root.has("condition")) {
            String value = root.path("condition").asText().trim();
            if (RegularUtil.isSelfPhone(value)) {
                query.setPhone(value);
            }
        }

        if (root.has("nameOrPhone")) {
            String value = root.path("nameOrPhone").asText().trim();
            if (RegularUtil.isSelfPhone(value)) {
                query.setPhone(value);
            }
        }else{
        }

        query.setCityId(validString(root, "city", 40, YesNoEnum.NO));
        query.setAgeId(validAgeId(root, YesNoEnum.NO));
        return query;
    }


//    public static ConsumerPrivateQO createQueryCondition(String json) throws Throwable {
//        ConsumerPrivateQO query = new ConsumerPrivateQO();
//        JsonNode root = validJson(json);
//        if (root == null)
//            return query;
//
//        if (root.has("condition")) {
//            String value = root.path("condition").asText().trim();
//            if (RegularUtil.isSelfPhone(value)) {
//                query.setPhone(validString(root, "phone", 11, YesNoEnum.NO));
//            }
//        }
//        query.setPhone(validString(root, "condition", 30, YesNoEnum.NO));
//        query.setCityId(validString(root, "city", 40, YesNoEnum.NO));
//        //query.setAge(validString(root, "age", 20, YesNoEnum.NO));
//        query.setAgeId(validAgeId(root, YesNoEnum.NO));
//        return query;
//    }



    public static Integer validAgeId(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "age";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if(StringUtils.isNotEmpty(value)) {
            	return Integer.valueOf(value);
            }else{
            	 throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            }
        }
        return 0;
    }

    public static ConsumerPrivateQO createQueryByOpenid(String json) throws Throwable {
        ConsumerPrivateQO query = new ConsumerPrivateQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;
        query.setOpenId(validString(root, "openid", 80, YesNoEnum.NO)).setPhone(validString(root, "phone", 11, YesNoEnum.NO));
        return query;
    }

    public static ConsumerPrivateQO createQueryBinDing(String json) throws Throwable {
        ConsumerPrivateQO query = new ConsumerPrivateQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setPhone(validString(root, "phone", 11, YesNoEnum.YES));
        return query;
    }


    public static ConsumerPrivateTO createTableForNew(String json) throws Throwable {
        ConsumerPrivateTO table = new ConsumerPrivateTO();
        JsonNode root = validJson(json);
        if (root == null) {
            throw new InputParamException(InputParamException.Type.INVALID, "参数");
        }
        table.setCareer(validString(root, "career", 40, YesNoEnum.NO)).setDistance(validString(root, "distance", 50, YesNoEnum.NO)
        ).setPhone(validString(root, "phone", 11, YesNoEnum.NO)).setCityId(validString(root, "city", 40, YesNoEnum.NO)).setSex(validSex(root, YesNoEnum.NO)).setAge(validString(root, "age", 20, YesNoEnum.NO));
        return table;
    }


    public static SexEnum validSex(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "sex";
        String value = node.path(key).asText().trim();
        if (value.equals("FEMALE") || value.equals("1")) {
            value = "1";
        } else if (value.equals("MALE") || value.equals("0")) {
            value = "0";
        } else {
            return null;
        }
        return SexEnum.getSex(value);
    }

}
